package com.stottlerhenke.simbionic.editor.parser;

import com.stottlerhenke.simbionic.api.SB_VarType;
import com.stottlerhenke.simbionic.common.parser.EUnaryOp;


public class SB_UnaryOpNode extends SB_ParseNode {
  private EUnaryOp _op;
  private SB_ParseNode _arg;

  public SB_UnaryOpNode(SB_ParseNode arg,EUnaryOp op) {
    _arg = arg;
    _op = op;
    // determine the result type of the operator
    if(_op == EUnaryOp.kNotOp)
      _type = SB_VarType.kBoolean;
    else if(_op == EUnaryOp.kNegateOp)
      _type = _arg.GetType();
    else if(_op == EUnaryOp.kParenOp)
      _type = _arg.GetType();
    else if(_op == EUnaryOp.kCastIntOp)
      _type = SB_VarType.kInteger;
    else if(_op == EUnaryOp.kCastFloatOp)
        _type = SB_VarType.kFloat;    
  }
  public SB_ErrorNode GetNextError(SB_ErrorNode lastError) {
    return _arg.GetNextError(lastError);
  }
  public boolean IsValid(){
    return _arg.IsValid();
  }
  public boolean ContainsVariable(String varName) {
    return _arg.ContainsVariable(varName);
  }
  public boolean ContainsFunction(String funcName) {
    return _arg.ContainsFunction(funcName);
  }
  public String ToString() {
    if(_op == EUnaryOp.kNotOp)
      return new String("!") + _arg.ToString();
    else if(_op == EUnaryOp.kNegateOp)
      return new String("-") + _arg.ToString();
    else if(_op == EUnaryOp.kParenOp)
      return new String("(") + _arg.ToString() + ")";
    else if(_op == EUnaryOp.kCastIntOp)
        return new String("(int)") + _arg.ToString();
    else if(_op == EUnaryOp.kCastFloatOp)
    	return new String("(float)") + _arg.ToString();
    else  
    	return new String("UNKNOWN UNARY-OP");
  }
  /**
   * @return the unary operator
   */
  EUnaryOp GetOperator() { return _op; }

  /**
   * @return the argument
   */
  SB_ParseNode GetArg() { return _arg; }

}